﻿using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;

namespace Mars.Pathfinding
{
    [BurstCompile]
    public struct FlowFieldIntegrationJob : IJob
    {
        [ReadOnly] public int GridWidth;
        [ReadOnly] public int GridHeight;
        [ReadOnly] public int2 Destination;
        [ReadOnly] public Native2DArray<bool> StaticObstacleGrid;
        [ReadOnly] public Native2DArray<bool> DynamicObstacleGrid;
        
        public Native2DArray<float> IntegrationField;
        public NativeList<CellPriority> PriorityQueue;

        public void Execute()
        {
            // 初始化集成场
            for (int y = 0; y < IntegrationField.Height; y++)
            {
                for (int x = 0; x < IntegrationField.Width; x++)
                {
                    IntegrationField[x, y] = float.MaxValue;
                }
            }

            // 目标点成本为0
            int destIndex = Destination.y * GridWidth + Destination.x;
            IntegrationField[Destination.x, Destination.y] = 0;
            PriorityQueue.Add(new CellPriority { Index = destIndex, Cost = 0 });

            while (PriorityQueue.Length > 0)
            {
                // 找到最小成本的单元格
                int minIndex = 0;
                float minCost = PriorityQueue[0].Cost;
                for (int i = 1; i < PriorityQueue.Length; i++)
                {
                    if (PriorityQueue[i].Cost < minCost)
                    {
                        minCost = PriorityQueue[i].Cost;
                        minIndex = i;
                    }
                }

                CellPriority cell = PriorityQueue[minIndex];
                PriorityQueue.RemoveAtSwapBack(minIndex);

                int x = cell.Index % GridWidth;
                int y = cell.Index / GridWidth;
                float currentCost = IntegrationField[x, y];

                // 检查所有邻居
                for (int dx = -1; dx <= 1; dx++)
                {
                    for (int dy = -1; dy <= 1; dy++)
                    {
                        if (dx == 0 && dy == 0)
                        {
                            continue;
                        }

                        int nx = x + dx;
                        int ny = y + dy;

                        // 检查邻居是否在网格范围内
                        if (nx < 0 || nx >= GridWidth || ny < 0 || ny >= GridHeight)
                        {
                            continue;
                        }

                        int neighborIndex = ny * GridWidth + nx;

                        // 跳过障碍物（静态或动态）
                        if (StaticObstacleGrid[nx, ny] || DynamicObstacleGrid[nx, ny]) continue;

                        // 计算移动成本（对角线成本更高）
                        float moveCost = (dx != 0 && dy != 0) ? 1.414f : 1f;
                        float newCost = currentCost + moveCost;

                        // 如果找到更低的成本路径
                        if (newCost < IntegrationField[nx, ny])
                        {
                            IntegrationField[nx, ny] = newCost;
                            PriorityQueue.Add(new CellPriority { Index = neighborIndex, Cost = newCost });
                        }
                    }
                }
            }
        }
    }
}